var util = require('../../../utils/util.js');
var api = require('../../../config/api.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    shopList: [],
    height:1,
    pageSize:10,
    pageNumber:1,
    shopName:"",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.getSystemInfo({
      success: (res) => {
        this.setData({
          height: res.windowHeight
        })
      }
    })
    this.getData(this.data.shopName);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  /**绑定搜索 */
  updateValue(e) {
    let name = e.detail.value;
    this.setData({
      shopName: name
    })
  },
  /**搜索 */
  search(){
    this.setData({
      pageNumber:1,
      pageSize:10,
      shopList:[]
    })
    this.getData(this.data.shopName);
  },
  /**查询数据 */
  getData(value){
    let that = this;
    let pageSize = this.data.pageSize;
    let pageNumber = this.data.pageNumber;
    let searchCondition = {
      pageSize: pageSize,
      pageNumber: pageNumber,
    }
    if (value != null && value != "") {
      searchCondition.conditionList = [{
        code: "productName",
        operator: 7,
        type: "String",
        value:value
      }]
    }
    wx.showLoading({
      title: '加载中',
    })
    util.request(api.ShopSearchUrl, searchCondition, 'POST', true).then(function (res) {
      if (res.code === 200 && res.result.list.length !=0) {
        let data = res.result.list;
        that.setData({
          shopList: that.data.shopList.concat(data),
          pageNumber: pageNumber +1,
        });
      } else if (res.code === 200 && res.result.list.length == 0){
        wx.showToast({
          title: '更有更多数据',
          icon: 'none',
          duration: 1000,
          mask: true
        })
      }else {
        wx.showModal({
          title: '提示',
          content: res.message,
          showCancel: false
        })
      }
      wx.hideLoading();
    });
  },
  /**加载更多 */
  getMore(){
    this.getData(this.data.shopName);
  },
  /**跳转 */
  goToDetail(e){
    if (e.currentTarget.dataset.id != undefined) {
      wx.navigateTo({
        url: '/myPages/shop/shopDetail/shopDetail?id=' + e.currentTarget.dataset.id
      })
    }
  }
})